package tuio.components;

import java.awt.Component;
import java.awt.Point;
import java.awt.Window;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import tuio.I_TouchListener;
import tuio.TCurAndPoint;
import tuio.TCurAndTouchListener;
import tuio.TuioControl;
import tuio.TuioTreatment;
import tuio.libTUIO.src.TUIO.TuioCursor;
import tuio.libTUIO.src.TUIO.TuioListener;
import tuio.libTUIO.src.TUIO.TuioObject;
import tuio.libTUIO.src.TUIO.TuioTime;

/**
 * @author Charles Perin
 * 9 juin 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class TuioFrame extends JFrame implements TuioListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4196236402426002649L;
	private TuioControl tuioControl;

	public TuioFrame(){
		super();
		this.tuioControl = new TuioControl();
	}

	public TuioFrame(String name) {
		super(name);
		this.tuioControl = new TuioControl();
	}




	@Override
	public void addTuioObject(TuioObject tobj) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateTuioObject(TuioObject tobj) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeTuioObject(TuioObject tobj) {
		// TODO Auto-generated method stub

	}

	public void addTuioCursor(TuioCursor tcur) {
		//System.err.println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY());
		int x = (int) (tcur.getX()*this.getWidth());
		int y = (int) (tcur.getY()*this.getHeight());
		Point localPoint = new Point(x,y);

		//Window topWindow = SwingUtilitiesPerso.getTopWindowContaining(this, x, y);
		//Point tmpPoint = SwingUtilities.convertPoint(this, new Point(x,y), this);
		Component c = SwingUtilities.getDeepestComponentAt(this, (int)localPoint.getX(), (int)localPoint.getY());

		//System.out.println();
		//System.out.println("window="+topWindow);
		//System.out.println("component="+c);
		//System.out.println("localpoint="+localPoint);

		if(c instanceof I_TouchListener){
			//Point localPoint = SwingUtilities.convertPoint(this, tmpPoint, c);
			if(TuioTreatment.isDoubleTouch(tuioControl.getPreviousTouch(), tcur)){
				//System.out.println("doubleclick in "+c);
				((I_TouchListener)c).touchDoubleClicked(new TCurAndPoint(tcur, localPoint));
			}
			else if(tuioControl.componentAlreadyHasATouch(c)){
				tuioControl.getTuioList().put(tcur.getCursorID(), new TCurAndTouchListener(new TCurAndPoint(tcur, localPoint), (I_TouchListener)c));
				//si deux touch
				if(tuioControl.getNbOfTouchInComponent(c)==2){
					ArrayList<Integer> list = tuioControl.getAllTouchIdInComponent(c);
					TCurAndPoint top1 = tuioControl.getTuioList().get(list.get(0)).gettCurAndPoint();
					TCurAndPoint top2 = tuioControl.getTuioList().get(list.get(1)).gettCurAndPoint();
					((I_TouchListener)c).doubleTouchPressed(top1, top2);
				}
			}
			else{
				tuioControl.getTuioList().put(tcur.getCursorID(), new TCurAndTouchListener(new TCurAndPoint(tcur, localPoint), (I_TouchListener)c));
				tuioControl.setPreviousTouch(new TuioCursor(tcur));
				((I_TouchListener)c).touchPressed(new TCurAndPoint(tcur, localPoint));
			}
		}
	}


	public void updateTuioCursor(TuioCursor tcur) {
		//System.out.println("set cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
		int x = (int) (tcur.getX()*this.getWidth());
		int y = (int) (tcur.getY()*this.getHeight());
		if(tuioControl.getTuioList().containsKey(tcur.getCursorID())){
			Component c = (Component) tuioControl.getTuioList().get(tcur.getCursorID()).getTouchListener();
			Window window = SwingUtilities.getWindowAncestor(c);
			Point tmpPoint = SwingUtilities.convertPoint(this, new Point(x,y), window);
			Point localPoint = SwingUtilities.convertPoint(window, tmpPoint, c);

			if(tuioControl.isLongPress(tcur)){
				tuioControl.getTuioList().get(tcur.getCursorID()).getTouchListener().longTouch(new TCurAndPoint(tuioControl.getTuioList().get(tcur.getCursorID()).gettCurAndPoint()));
				TuioTime.initSession();
			}
			else{
				tuioControl.getTuioList().get(tcur.getCursorID()).gettCurAndPoint().gettCur().update(tcur);
				tuioControl.getTuioList().get(tcur.getCursorID()).gettCurAndPoint().setLocalPoint(localPoint);
				if(tuioControl.getNbOfTouchInComponent(c)==2){
					ArrayList<Integer> list = tuioControl.getAllTouchIdInComponent(c);
					TCurAndPoint top1 = new TCurAndPoint(tuioControl.getTuioList().get(list.get(0)).gettCurAndPoint());
					TCurAndPoint top2 = new TCurAndPoint(tuioControl.getTuioList().get(list.get(1)).gettCurAndPoint());
					((I_TouchListener)c).doubleTouchDragged(top1, top2);
				}
				else{
					tuioControl.getTuioList().get(tcur.getCursorID()).getTouchListener().touchDragged(new TCurAndPoint(tuioControl.getTuioList().get(tcur.getCursorID()).gettCurAndPoint()));
				}
			}
		}
	}

	public void removeTuioCursor(TuioCursor tcur) {
		//System.out.println("del cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");	
		int x = (int) (tcur.getX()*this.getWidth());
		int y = (int) (tcur.getY()*this.getHeight());
		if(tuioControl.getTuioList().containsKey(tcur.getCursorID())){
			Component c = (Component) tuioControl.getTuioList().get(tcur.getCursorID()).getTouchListener();
			Window window = SwingUtilities.getWindowAncestor(c);
			if(window!=null){
				Point tmpPoint = SwingUtilities.convertPoint(this, new Point(x,y), window);
				Point localPoint = SwingUtilities.convertPoint(window, tmpPoint, c);
				if(TuioTreatment.isSingleClick(tcur)){
					//System.out.println("simpleClick in "+c);
					((I_TouchListener)c).touchClicked(new TCurAndPoint(tcur, localPoint));
				}
				else{
					tuioControl.getTuioList().get(tcur.getCursorID()).getTouchListener().touchReleased(new TCurAndPoint(tcur, localPoint));
				}
			}
			
			tuioControl.getTuioList().remove(tcur.getCursorID());
			//System.err.println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY());
		}
	}

	@Override
	public void refresh(TuioTime ftime) {
		// TODO Auto-generated method stub

	}

}
